<?php
namespace Custom\Controller;
use Think\Controller;

class TablecateController extends UserController{
    function __construct()
    {
        parent::__construct();
        $this->M_table_cate = M('Table_cate');
    }
    public function index()
    {
        $sid = session('s_id');
        if($sid){
            $p = I('p');
            $page_size = '10';
            $count = $this->M_table_cate->where('s_id=' . $sid)->count();
            $list = $this->M_table_cate->where('s_id='.$sid)->page($p, $page_size)->select();
            $Page = new \Think\Page($count, $page_size);// 实例化分页类 传入总记录数和每页显示的记录数
            $show = $Page->show();// 分页显示输出
            $this->assign('show', $show);// 赋值分页输出
            $this->assign('list',$list);
        }
        $this->display();
    }
    //添加餐桌分类
    public function addTable()
    {
        if(!$this->s_id){
            die($this->error('请先选择门店！',U('goods/index')));
        }else{
            $info = M('store')->field('store_name,s_id')->select();
            $this->assign('info',$info);
            if(IS_POST){
                $data['s_id'] = $this->s_id;
                $data['table_cate_name'] = trim(I('post.table_cate_name'));
                $data['sit_people'] = trim(I('post.sit_people'));
                $res = $this->M_table_cate->add( $data );
                if($res){
                    $this->success('success',U('Tablecate/index'));
                }else{
                    $this->error('failed');
                }
            }
        }
        $this->display();
    }
    //编辑餐桌分类
    public function edit()
    {
        $id = I('get.id');
        $info = $this->M_table_cate->find($id);
        $this->assign('info',$info);
        if(IS_POST){
            $data['table_cate_id'] = I('post.cate_id');
            $data['table_cate_name'] = trim(I('table_cate_name'));
            $data['sit_people'] = trim(I('sit_people'));
            $res = $this->M_table_cate->save( $data );
            if($res){
                $this->success('success',U('Tablecate/index'));
            }else{
                $this->error('failed');
            }
        }else{
            $this->display();
        }
    }
    /**
     *  删除分类
     */
    public function delete(){
        $id = (int)$_GET['id'];
        $res = $this->M_table_cate->delete($id);
        if($res){
            $this->ajaxReturn(array('result'=>'tp1'));
        }else{
            $this->ajaxReturn(array('result'=>'break'));
        }
    }
}